var that
const app = getApp()
const db = wx.cloud.database();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    top:0,
    totalCount: 0,
    totalCount2:0,
    totalCount3: 0,
    totalCount4:0,
    totalCount5: 0,
    topics: [],
    follows:{},
    flat:1,
    _id:[],
    avatarUrl:'',
    color0:1,
    color1:'#000000',
    color2:'#b6b6b6',
    color3:'#b6b6b6',
    color4:'#b6b6b6',
    color5:'#b6b6b6',
    imgUrls: [
      '../../images/index/1.jpg',
      '../../images/index/2.jpg',
     '../../images/index/3.jpg',
     '../../images/index/4.jpg',
     '../../images/index/5.jpg',
      ],
      indicatorDots: true,
      autoplay: true,
      interval: 5000,
      duration: 1000,
      f :1,
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    wx.cloud.init({
      env: 'test-5gwxfg0see31cde1',
    })
    db.collection('topic').count().then(
      res=>{
        that.data.totalCount=res.total
        that.setData({
          totalCount : that.data.totalCount
        })
      }
    )
    db.collection('topic')
    .where({
      'label':"1"
   })
    .count().then(
      res=>{
        that.data.totalCount4=res.total
        that.setData({
          totalCount4 : that.data.totalCount4
        })
      }
    )
    db.collection('topic')
    .where({
      'label':"2"
   })
    .count().then(
      res=>{
        that.data.totalCount5=res.total
        that.setData({
          totalCount5 : that.data.totalCount5
        })
      }
    )
   // that.time();
  },
  jugdeUserLogin: function(e) {
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res){
              app.globalDate.user= res.userInfo;
             console.log(app.globalDate.user)
            }
          })
        }
      }
      
    })
  },
  scrollTopFun(e){
    let that = this;
    that.top = e.detail.scrollTop;
    that.$apply();
  },
  onShow: function() {
    that.getData();
    that.time();
  },
  /**
   * 获取列表数据
   * 
   */
  getData: function() {
    db.collection('topic')
      .orderBy('date', 'desc')
      .get({
        success: function(res) {
          // res.data 是包含以上定义的两条记录的数组 
          console.log("数据：" + res.data)
          that.data.topics = res.data;
          that.setData({
            topics: that.data.topics,
          })
          wx.hideNavigationBarLoading(); //隐藏加载
          wx.stopPullDownRefresh();
        },
        fail: function(event) {
          wx.hideNavigationBarLoading(); //隐藏加载
          wx.stopPullDownRefresh();
        }
      })
  },
  /**
   *排序方式时间
   */
  time:function(e){
    that.data.f = 1;
    that.data.topics = [];
    that.setData({
      f: that.data.f,
      topics:that.data.topics
    })
    console.log(that.data.f)
    if(that.data.color0!=1){
      that.data.color0=1;
      that.data.color1='#000000';
      that.data.color2='#b6b6b6';
      that.data.color3='#b6b6b6';
      that.data.color4='#b6b6b6';
      that.data.color5='#b6b6b6';
      that.setData({
        color1:'#000000',
        color2:'#b6b6b6',
        color3:'#b6b6b6',
        color4:'#b6b6b6',
        color5:'#b6b6b6',
      }),
      that.getData();
    } 
  
  },
  /**
   *点赞数
   */
  pionts:function(e){
    var temp = [];
    that.data.f = 2;
    that.data.topics = [];
    that.setData({
      f: that.data.f,
      topics:  that.data.topics
    })
    if(that.data.color0 != 2){
      that.data.color0=2;
      that.data.color1='#b6b6b6';
      that.data.color2='#000000';
      that.data.color3='#b6b6b6';
      that.data.color4='#b6b6b6';
      that.data.color5='#b6b6b6';
      that.setData({
        color1:'#b6b6b6',
        color2:'#000000',
        color3:'#b6b6b6',
        color4:'#b6b6b6',
        color5:'#b6b6b6',
      }),
      db.collection('topic')
    .orderBy('isLike', 'desc')
    .orderBy('date', 'desc')
  //返回数据
    .get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        console.log("数据：" + res.data)
        that.data.topics = res.data;
        that.setData({
          topics: that.data.topics,
        })
        wx.hideNavigationBarLoading(); //隐藏加载
        wx.stopPullDownRefresh();

      },
      fail: function(event) {
        wx.hideNavigationBarLoading(); //隐藏加载
        wx.stopPullDownRefresh();
      }
    })
    }
    
   
  },
   /**
   *关注
   */
  collectep1:function(e) {
    that.data.f = 3;
    that.data.topics = {};
    that.setData({
      f : that.data.f,
      topics:  that.data.topics
    })
    if(that.data.color0 != 3){
      that.data.color0=3;
      that.data.color1='#b6b6b6';
      that.data.color2='#b6b6b6';
      that.data.color3='#000000';
      that.data.color4='#b6b6b6';
      that.data.color5='#b6b6b6';
      that.setData({
        color1:'#b6b6b6',
        color2:'#b6b6b6',
        color3:'#000000',
        color4:'#b6b6b6',
        color5:'#b6b6b6',
      })
    var id ;
    db.collection('follow').field({
      f_id : true
    })
    .get({ 
      success:function (res) {
        that.data._id = res.data;
        console.log(res.data)
        const _ = db.command
         id= new Array(res.data.length);
        for(var i=0;i<res.data.length;i++){
          console.log(i);
          id[i] = that.data._id[i].f_id; 
        }
        console.log(id);
   db.collection('topic').where({
       '_openid' : _.in(id)
   })
  .orderBy('date', 'desc')
  .get({
     success:function (res) {
     console.log("成功调用第二个函数")
     console.log(res.data)
      that.data.topics = res.data;
      that.setData({
         topics: that.data.topics,
       })
       if(that.data.topics.length==0){
        wx.showToast({
          icon: 'none',
          title: '你暂未关注任何人',
        })
      }
     },
     fail: console.error
   })
      }
   })
    }
  
    
  },
  // collection函数
  collection:function(e){
    const _ = db.command
    console.log(that.data._id)
    db.collection('topic').where({
      '_openid': that.data._id[0]
    })
   .get({
      success:function (res) {
      console.log("成功调用第二个函数")
      console.log(res.data)
       that.data.topics = res.data;
       that.setData({
          topics: e.data.topics,
        })
      },
      fail: console.error
    })
   },

  /**
   *暴食
   */
  gluttony:function(e){
    that.data.f = 4;
    that.data.topics = {};
    that.setData({
      f : that.data.f,
      topics:  that.data.topics 
    })
    if(that.data.color0 != 4){
      that.data.color0=4;
      that.data.color1='#b6b6b6';
      that.data.color4='#000000';
      that.data.color3='#b6b6b6';
      that.data.color2='#b6b6b6';
      that.data.color5='#b6b6b6';
      that.setData({
        color1:'#b6b6b6',
        color4:'#000000',
        color3:'#b6b6b6',
        color2:'#b6b6b6',
        color5:'#b6b6b6',
      }),
    db.collection('topic') .where({
       'label':"1"
    })
    .orderBy('date', 'desc')
    .get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        console.log("数据：" + res.data)
        that.data.topics = res.data;
        that.setData({
          topics: that.data.topics,
        })
        wx.hideNavigationBarLoading(); //隐藏加载
        wx.stopPullDownRefresh();
      },
      fail: function(event) {
        wx.hideNavigationBarLoading(); //隐藏加载
        wx.stopPullDownRefresh();
      }
    })
  }
  },
  /**
   *帮助
   */
  advise:function(e){
    that.data.f = 5;
    that.data.topics = {};
    that.setData({
      f : that.data.f,
      topics:  that.data.topics 
    })
    if(that.data.color0 != 5){
      that.data.color0=5;
      that.data.color1='#b6b6b6';
      that.data.color5='#000000';
      that.data.color3='#b6b6b6';
      that.data.color4='#b6b6b6';
      that.data.color2='#b6b6b6';
      that.setData({
        color1:'#b6b6b6',
        color5:'#000000',
        color3:'#b6b6b6',
        color4:'#b6b6b6',
        color2:'#b6b6b6',
      }),
    db.collection('topic') .where({
      'label':"2"
   })
   .orderBy('date', 'desc')
   .get({
     success: function(res) {
       // res.data 是包含以上定义的两条记录的数组
       console.log("数据：" + res.data)
       that.data.topics = res.data;
       that.setData({
         topics: that.data.topics,
       })
       wx.hideNavigationBarLoading(); //隐藏加载
       wx.stopPullDownRefresh();
     },
     fail: function(event) {
       wx.hideNavigationBarLoading(); //隐藏加载
       wx.stopPullDownRefresh();
     }
   })
  }
  },
  /**
   * item 点击
   */
  onItemClick: function(event) {
    var id = event.currentTarget.dataset.topicid;
    var openid = event.currentTarget.dataset.openid;
    var count = event.currentTarget.dataset.count + 1;
    var isLike = event.currentTarget.dataset.islike;
    console.log(id);
    console.log(openid);
    console.log(count);
    console.log(isLike);
    wx.navigateTo({
      url: "../detail/detail?id=" + id + "&openid=" + openid +"&count=" + count+"&isLike=" + isLike
    })
  },
  file:function(){
    wx.navigateTo({
      url: "../file/file"
    })
  },
  offical:function(){
    wx.navigateTo({
      url: "../offical_sharing/offical_sharing"
    })
  },
  userInfo:function(){
    wx.navigateTo({
      url: "../me/me"
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log(that.data.f)
    if(that.data.f==1){
      var temp = [];
      // 获取后面十条
      console.log(this.data.topics.length);
      console.log(this.data.totalCount);
      if (this.data.topics.length < this.data.totalCount) {
        const db = wx.cloud.database();
        db.collection('topic')
        .orderBy('date', 'desc')
        .skip(this.data.topics.length)
        .get({
          success: function(res) {
            // res.data 是包含以上定义的两条记录的数组
            if (res.data.length > 0) {
              for (var i = 0; i < res.data.length; i++) {
                var tempTopic = res.data[i];
                console.log(tempTopic);
                temp.push(tempTopic);
              }
              var totalTopic = {};
              totalTopic = that.data.topics.concat(temp);
              console.log(totalTopic);
              that.setData({
                topics: totalTopic,
              })
            } else {
              wx.showToast({
                title: '没有更多数据了',
              })
            }
          
          },
        })
      } else {
        wx.showToast({
          title: '没有更多数据了',
        })
      }
    }
    else if(that.data.f==2){
      var temp = [];
      // 获取后面十条
      console.log(this.data.topics.length);
      console.log(this.data.totalCount);
      if (this.data.topics.length < this.data.totalCount) {
        const db = wx.cloud.database();
        db.collection('topic')
        .orderBy('isLike', 'desc')
        .orderBy('date', 'desc')
        .skip(this.data.topics.length)
        .get({
          success: function(res) {
            // res.data 是包含以上定义的两条记录的数组
            if (res.data.length > 0) {
              for (var i = 0; i < res.data.length; i++) {
                var tempTopic = res.data[i];
                console.log(tempTopic);
                temp.push(tempTopic);
              }
              var totalTopic = {};
              totalTopic = that.data.topics.concat(temp);
              console.log(totalTopic);
              that.setData({
                topics: totalTopic,
              })
            } else {
              wx.showToast({
                title: '没有更多数据了',
              })
            }
          
          },
        })
      } else {
        wx.showToast({
          title: '没有更多数据了',
        })
      }
    }
   else if(that.data.f ==4){
    var temp = [];
    // 获取后面十条
    console.log(this.data.topics.length);
    console.log(this.data.totalCount4);
    if (this.data.topics.length < this.data.totalCount4) {
      const db = wx.cloud.database();
      db.collection('topic')
      .where({
        'label':"1"
     })
     .orderBy('date', 'desc')
     .skip(this.data.topics.length)
      .get({
        success: function(res) {
          // res.data 是包含以上定义的两条记录的数组
          if (res.data.length > 0) {
            for (var i = 0; i < res.data.length; i++) {
              var tempTopic = res.data[i];
              console.log(tempTopic);
              temp.push(tempTopic);
            }
            var totalTopic = {};
            totalTopic = that.data.topics.concat(temp);
            console.log(totalTopic);
            that.setData({
              topics: totalTopic,
            })
          } else {
            wx.showToast({
              title: '没有更多数据了',
            })
          }
        
        },
      })
    } else {
      wx.showToast({
        title: '没有更多数据了',
      })
    }
  }else if(that.data.f ==5){
    var temp = [];
    // 获取后面十条
    console.log(this.data.topics.length);
    console.log(this.data.totalCount5);
    if (this.data.topics.length < this.data.totalCount5) {
      const db = wx.cloud.database();
      db.collection('topic')
      .where({
        'label':"2"
     })
     .orderBy('date', 'desc')
     .skip(this.data.topics.length)
      .get({
        success: function(res) {
          // res.data 是包含以上定义的两条记录的数组
          if (res.data.length > 0) {
            for (var i = 0; i < res.data.length; i++) {
              var tempTopic = res.data[i];
              console.log(tempTopic);
              temp.push(tempTopic);
            }
            var totalTopic = {};
            totalTopic = that.data.topics.concat(temp);
            console.log(totalTopic);
            that.setData({
              topics: totalTopic,
            })
          } else {
            wx.showToast({
              title: '没有更多数据了',
            })
          }
        
        },
      })
    } else {
      wx.showToast({
        title: '没有更多数据了',
      })
    }
  }else if(that.data.f ==3 || that.data.f ==2){
    wx.showToast({
      title: '没有更多数据了',
    })
  }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }

  
})